package com.lvcoding.sec3.secdemo3.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


public class ResponseUtil {

    public static void JsonResult(HttpServletResponse response, int code, String msg, Object data) throws IOException {
        response.setContentType("application/json;charset=utf8");
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> map = new HashMap<>(3);
        map.put("code", code);
        map.put("data", data);
        map.put("msg", msg);
        String result = objectMapper.writeValueAsString(map);
        PrintWriter writer = response.getWriter();
        writer.write(result);
        writer.flush();
    }
}
